package com.anolesoft.yr.mbus.part.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.part.dto.PartModelDTO;
import com.anolesoft.yr.mbus.part.dto.PartSafetyQuantityDTO;
import com.anolesoft.yr.mbus.part.service.PartSafetyQuantityService;
import com.anolesoft.yr.mbus.part.vo.PartModelVO;
import com.anolesoft.yr.mbus.part.vo.PartSafetyQuantityVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/11 19:25
 */
@RestController
public class PartSafetyQuantityController {
    @Autowired
    private PartSafetyQuantityService partSafetyQuantityService;

//    private PartSafetyQuantityVO dto2vo(PartSafetyQuantityDTO t) {
//        PartModelDTO partModel = t.getPartModel();
//        PartModelVO partModelVO = null;
//        if (partModel != null) {
//            partModelVO = CustomBeanUtils.partModelDTO2VO(partModel);
//        } else {
//            partModelVO = PartModelVO.builder().build();
//        }
//        return PartSafetyQuantityVO.builder()
//                .objuid(t.getObjuid())
//                .stkMainUid(t.getStkMainUid())
//                .partModelUid(t.getPartModelUid())
//                .factoryCode(t.getFactoryCode())
//                .warningQuantity(t.getWarningQuantity())
//                .remark(t.getRemark())
//                .modifyUserUid(t.getModifyUserUid())
//                .modifyTime(t.getModifyTime())
//                .lowestReserve(t.getLowestReserve())
//                .highestReserve(t.getHighestReserve())
//                .deleteFlag(t.getDeleteFlag())
//                .createUserUid(t.getCreateUserUid())
//                .createTime(t.getCreateTime())
//                .partModel(partModelVO)
//                .build();
//    }

    private PartSafetyQuantityDTO vo2dto(PartSafetyQuantityVO partSafeQuantityVO, boolean newId) {
        return PartSafetyQuantityDTO.builder()
                .objuid(newId ? CommonUtils.fastUUID() : partSafeQuantityVO.getObjuid())
                .highestReserve(partSafeQuantityVO.getHighestReserve())
                .lowestReserve(partSafeQuantityVO.getLowestReserve())
                .warningQuantity(partSafeQuantityVO.getWarningQuantity())
                .partModelUid(partSafeQuantityVO.getPartModelUid())
                .stkMainUid(partSafeQuantityVO.getStkMainUid())
                .factoryCode(partSafeQuantityVO.getFactoryCode())
                .remark(partSafeQuantityVO.getRemark())
                .modifyUserUid(SecurityUtils.getUserId())
                .createUserUid(SecurityUtils.getUserId())
                .createTime(partSafeQuantityVO.getCreateTime())
                .deleteFlag(partSafeQuantityVO.getDeleteFlag())
                .modifyTime(partSafeQuantityVO.getModifyTime())
                .build();
    }

    @PostMapping(value = "/api/part/partsafetyquantity/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(PartSafetyQuantityVO partModelVO) {
        PartSafetyQuantityDTO partModelDTO = vo2dto(partModelVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, partSafetyQuantityService.create(partModelDTO));
    }


    @PutMapping(value = "/api/part/partsafetyquantity/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(PartSafetyQuantityVO partSafetyQuantityVO) {
        PartSafetyQuantityDTO partSafetyQuantityDTO = vo2dto(partSafetyQuantityVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, partSafetyQuantityService.modify(partSafetyQuantityDTO));
    }

    @DeleteMapping(value = "/api/part/partsafetyquantity/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, partSafetyQuantityService.remove(id));
    }

    @DeleteMapping(value = "/api/part/partsafetyquantity/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult removeBatch(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, partSafetyQuantityService.removeBatch(ids));
    }

    @GetMapping(value = "/api/part/partsafetyquantity/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<PartSafetyQuantityDTO> partSafetyQuantityDTO = this.partSafetyQuantityService.selectOneById(id);
        if (partSafetyQuantityDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.partSafetyQuantityDTO2VO(partSafetyQuantityDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }

    }

    @GetMapping(value = "/api/part/partsafetyquantity/list/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll() {
        List<PartSafetyQuantityVO> vos = partSafetyQuantityService.selectAll().stream().map(t -> CustomBeanUtils.partSafetyQuantityDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartSafetyQuantityVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/partsafetyquantity/list/all/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageAll(
            @RequestParam("rows") Integer limit,
            @RequestParam("page") Integer curr) {
        PageData<List<PartSafetyQuantityDTO>> pageData = partSafetyQuantityService.selectAll(limit, curr);
        List<PartSafetyQuantityVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.partSafetyQuantityDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartSafetyQuantityVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/partsafetyquantity/list/condition", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(PartSafetyQuantityVO partSafetyQuantityVO) {
        PartSafetyQuantityDTO partSafetyQuantityDTO = vo2dto(partSafetyQuantityVO, false);
        List<PartSafetyQuantityVO> vos = this.partSafetyQuantityService.selectList(partSafetyQuantityDTO).stream().map(t -> CustomBeanUtils.partSafetyQuantityDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartSafetyQuantityVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/partsafetyquantity/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectListPage(PartSafetyQuantityVO partSafetyQuantityVO) {
        PartSafetyQuantityDTO partModelDTO = vo2dto(partSafetyQuantityVO, false);
        Integer limit = partSafetyQuantityVO.getRows();
        Integer curr = partSafetyQuantityVO.getPage();
        PageData<List<PartSafetyQuantityDTO>> pageData = partSafetyQuantityService.selectList(partModelDTO, limit, curr);
        List<PartSafetyQuantityVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.partSafetyQuantityDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartSafetyQuantityVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }
}
